package com.example.easypad;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.Menu;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.navigation.NavigationView;

import androidx.appcompat.app.AlertDialog;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;

import com.example.easypad.databinding.ActivityMainBinding;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText et_username, et_pwd;
    public static SQLiteOpenHelper helper = null;
    public static Context mainContext = null;
    public static SQLiteDatabase db;

    public static String userName,passWord,uid,sql = "";
    private TextView tv_titleUserName;
    private Cursor cursor = null;
    public static MyAdapter myAdapter;
    private boolean isExist = false;
    private AppBarConfiguration mAppBarConfiguration;
    private ActivityMainBinding binding;
    private ImageView ic_showitemsmenu;
    public static ListView lv_notes;
    public static List<Notes> data = new ArrayList<>();
    private User user;
    private void initLayout() {
        tv_titleUserName = findViewById(R.id.tv_titleUserName);
        lv_notes = findViewById(R.id.lv_notes);
//        ic_showitemsmenu = findViewById(R.id.ic_showitemsmenu);
        mainContext = getApplicationContext();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MakeStatusBarTransparent.makeStatusBarTransparent(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        helper = new ContactSQLiteOpenHelper(this,"demo.db",null,2);
        db = helper.getWritableDatabase();
        initLayout();
        myAdapter = new MyAdapter(data,mainContext);
        lv_notes.setAdapter(myAdapter);

        uid = getIntent().getStringExtra("uid");
        userName = getIntent().getStringExtra("userName");
        passWord = getIntent().getStringExtra("passWord");
        user = new User(uid,userName,passWord);
        showData();
        lv_notes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(MainActivity.this,activity_item.class);
                intent.putExtra("index",i + "");
                intent.putExtra("id",data.get(i).getId() + "");
                intent.putExtra("title",data.get(i).getTitle());
                intent.putExtra("context",data.get(i).getContext());
                intent.putExtra("type",data.get(i).getType());
                intent.putExtra("dateTime",data.get(i).getDateTime());
                startActivity(intent);

            }
        });



        setSupportActionBar(binding.appBarMain.toolbar);
        binding.appBarMain.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this,activiti_new_Edit.class);
//                Intent intent = new Intent(MainActivity.this,activity_test.class);
                startActivity(intent);
            }
        });
        DrawerLayout drawer = binding.drawerLayout;
        NavigationView navigationView = binding.navView;
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
//        mAppBarConfiguration = new AppBarConfiguration.Builder(
//                R.id.nav_home, R.id.nav_life, R.id.nav_work)
//                .setOpenableLayout(drawer)
//                .build();
//        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
//        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
//        NavigationUI.setupWithNavController(navigationView, navController);
    }

    public static String getDateTime() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return format.format(date);
    }

    @SuppressLint("Range")
    public static void showData() {
        data.clear();
//        sql = "SELECT * FROM Notes WHERE userName=?";
//        Cursor cursor = db.rawQuery(sql,new String[]{userName});
        Cursor cursor = db.query("Notes",null,"userName = ?",new String[]{userName},null,null,null);
//        Cursor cursor = db.query("Notes",null,null,null,null,null,null);
        if (cursor.moveToNext()) {
            do {
                String id = cursor.getString(cursor.getColumnIndex("id"));
                String title = cursor.getString(cursor.getColumnIndex("title"));
                String context = cursor.getString(cursor.getColumnIndex("context"));
                String type = cursor.getString(cursor.getColumnIndex("type"));
                String dateTime = cursor.getString(cursor.getColumnIndex("dateTime"));
                Notes notes = new Notes(id,title,context,type,dateTime);
                data.add(notes);

                Log.d("MainActivity",id);
                Log.d("MainActivity",title);
                Log.d("MainActivity",context);
                Log.d("MainActivity",type);
                Log.d("MainActivity",dateTime);
            }while (cursor.moveToNext());
        }
        cursor.close();

    }

    private void init() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);

        menu.getItem(1).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.action_about:{
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("关于");
                        builder.setMessage("EasyPad安卓版,项目已开源，详情点击“了解更多");
                        builder.setPositiveButton("了解更多", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                Uri uri = Uri.parse("https://wd_moonmoonbird.gitee.io/easypad/");
                                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                                startActivity(intent);
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                        break;
                    }
                }
                return false;
            }
        });

        menu.getItem(0).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.action_account:{
                        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("账户");
                        builder.setMessage(userName);
                        builder.setPositiveButton("退出登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                data.clear();
                                db.close();
                                Intent intent = new Intent(MainActivity.this,activity_login.class);
                                startActivity(intent);
                                finish();
                            }
                        });
                        AlertDialog dialog = builder.create();
                        dialog.show();
                        break;
                    }

                }

                return false;
            }
        });
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }
}